import userSupervisorExtApi from '@supportApi/obExt'

export const staffMixin = {
  data() {
    return {
      // 在线监督员点集
      staffMarkers: []
    }
  },
  methods: {
    /**
     * @Author ling.yuan@topevery.club
     * @Date 2019年12月9日 16:29:54
     * @Param event 是否开启人员图层(true/false)
     * @Description 加载在线监督员数据并添加至地图
     */
    initStaffFeature(event, target = 'staff') {
      if (event) {
        const query = {
          page: false,
          params: [],
          userDbStatus: '1',
          userOnlineStatus: '1'
        }
        userSupervisorExtApi.listDetailInfoByCondition(query)
          .then(res => {
            // 监督员数据清空
            this.staffMarkers = []

            if (res.data.length > 0) {
              res.data.forEach(item => {
                // 坐标有效且监督员在线
                if (item.gpsLastOnlinePosition &&
                  item.gpsLastOnlinePosition !== '[0,0]' &&
                  item.gpsLastOnlinePosition !== '[null,null]' &&
                  item.online === '1') {
                  // 坐标点
                  let po = item.gpsLastOnlinePosition
                  po = po.replace('[', '')
                  po = po.replace(']', '')
                  po = po.split(',')
                  const obj = {
                    id: item.userId,
                    title: item.userName,
                    target: target,
                    coordinates: [Number(po[0]), Number(po[1])],
                    animate: true,
                    stopEvent: true,
                    data: item
                  }
                  this.staffMarkers.push(obj)
                }
              })
              this.showMarkerFeature(this.staffMarkers, false, target)
            } else {
              this.$message.warning('暂无在线人员')
            }
          })
          .catch(() => {

          })
      } else {
        // 清除图层feature
        if (this.staffMarkers.length > 0) {
          this.clearMarkerFeature(target)
          this.$refs.staffBar.handleStaffAllHide()
        }
      }
    },
    /**
     * @Author ling.yuan@topevery.club
     * @Date 2019/12/09 14:48:39
     * @Description 人员点击展示详情
     */
    handleStaffClick(event) {
      this.$refs.staffBar.handleShowStaffDetail(event.userId)
      this.$refs.caseBar.handleCloseCaseInfo()
      this.$refs.videoBar.handleCloseVideoDetail()
      this.$refs.carBar.handleCloseCarDetail()
    },
    /**
     * @Author ling.yuan@topevery.club
     * @Date 2019/12/09 17:10:35
     * @Description 定位
     */
    handleStaffPosition(event, target = 'staff') {
      if (event.online !== '1') {
        this.$message.warning('该人员未在线,暂无定位信息')
        return
      }
      for (let i = 0; i < this.staffMarkers.length; i++) {
        if (this.staffMarkers[i].id === event.userId) {
          this.handleItemSelect(this.staffMarkers[i], false, target, false)
          break
        }
      }
    }
  }
}
